package com.unionpay.logan.controller;

import com.unionpay.logan.utils.LoganParser;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.io.*;

/**
 * Created by wxj on 2018/11/26 10:41
 */

@RestController
public class LoganController {

    @Value("${file.path}")
    private String file;

    @Value("${aes.key}")
    private String key;

    @Value("${aes.iv}")
    private String iv;

    @PostMapping("/logupload")
    public BaseResponse upload(HttpServletRequest request){
        try {
            LoganParser loganParser = new LoganParser(key.trim().replace("\"","").getBytes(),iv.trim().replace("\"","").getBytes());
            OutputStream os =new FileOutputStream(file);
            loganParser.parse(request.getInputStream(),os);
            os.flush();
            os.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return new BaseResponse.Builder()
                .code("0000")
                .msg("success")
                .build();
    }

}
